import { action, observable, computed } from 'mobx';
import * as Api from '../apis/customPageApi';
import { WeaForm, WeaTableNew } from 'comsMobx';
import { WeaLocaleProvider } from 'ecCom';
import { message } from 'antd';

const { getLabel } = WeaLocaleProvider;
const { TableStore } = WeaTableNew;

export default class CustomPageStore {
  @observable workflowid = '';
  @observable operationType = '0';
  @observable requestid = ''
  @observable superviseVisible = false;
  @observable superviseForm = new WeaForm();// 督办
  @observable superviseconditions = [];
  @observable urgeForm = new WeaForm();// 催办
  @observable urgeVisible = false;
  @observable delayRequestForm = new WeaForm();
  @observable delayRequestVisible = false;
  @observable superviseid = '';
  @observable detailForm = new WeaForm();
  @observable detailTabs = []
  @observable detailSelectKey = '0';
  @observable tableStore1 = new TableStore();
  @observable tableStore2 = new TableStore();
  @observable tableStore3 = new TableStore();
  @observable detailDialogVisible = false;
  @observable remindSetingVisible = false;
  @observable remindSetingForm = new WeaForm();
  @observable processOpinionVisible = false;
  @observable opinionList = [];
  @observable isOpen = true; // 是否启用地址
  @observable customListId = ''// 自定义页面id
  @observable customTabId = ''
  @observable pageTitle = ''// 页面标题
  @observable getButtonType = '';
  @observable buttonType = '';
  @observable sharearg = {};// 批量提交相关权限
  @observable showBatchSubmit = false;
  @observable selectedKeys = [];
  @observable selectedDatas = [];

  @computed get tableParams() {
    return ({ customListId: this.customListId, btnType: this.buttonType });
  }

  @action.bound
  onSupervise() {
    const params = {
      workflowid: this.workflowid,
      operationType: this.operationType,
    };
    Api.getSuperviseOperation(params).then((data) => {
      if (data.formConditions) {
        this.superviseconditions = [];
        this.superviseForm = new WeaForm();
        this.superviseconditions = data.formConditions;
        this.superviseForm.initFormFields(data.formConditions);
        this.remindSetingForm.setCondition(data.setConditions);
        this.superviseVisible = true;
      }
    });
  }

  @action.bound
  reminder() {
    const params = {
      requestid: this.requestid,
      operationType: this.operationType,
      superviseid: this.superviseid,
    };
    Api.getSuperviseOperation(params).then((data) => {
      if (data.conditions) {
        this.urgeForm = new WeaForm();
        this.urgeForm.setCondition(data.conditions);
        this.urgeVisible = true;
      }
    });
  }

  @action.bound
  checkDetails() {
    const params = {
      requestid: this.requestid,
      operationType: this.operationType,
      superviseid: this.superviseid,
    };
    Api.getSuperviseOperation(params).then((data) => {
      this.detailForm = new WeaForm();
      this.tableStore1 = new TableStore();
      this.tableStore2 = new TableStore();
      this.tableStore3 = new TableStore();
      this.detailForm.setCondition(data.conditions);
      this.detailTabs = data.topTablist;
      this.tableStore1.getDatas(data.datalists.remindTableKey);
      this.tableStore2.getDatas(data.datalists.urgeTableKey);
      this.tableStore3.getDatas(data.datalists.delayTableKey);
      this.detailDialogVisible = true;
    });
  }

  @action.bound
  cancelReminder(getTableDatas, getTabData) {
    const params = {
      requestid: this.requestid,
      operationType: this.operationType,
      superviseid: this.superviseid,
    };
    Api.saveSuperviseOperation(params).then((data) => {
      if (data.api_status) {
        message.success(getLabel('129248', '取消成功'));
        getTableDatas({}, false, false);
        getTabData();
      }
    });
  }

  @action.bound
  delayDate(getTableDatas) {
    const params = {
      workflowid: this.workflowid,
      operationType: this.operationType,
      requestid: this.requestid,
      superviseid: this.superviseid,
      ...this.delayRequestForm.getFormParams(),
    };
    Api.saveSuperviseOperation(params).then((data) => {
      if (data.api_status) {
        message.success(getLabel('517320', '延期处理申请提交成功'));
        this.delayRequestVisible = false;
        getTableDatas({}, false, false);
      }
    });
  }

  @action.bound
  superviseHide() {
    this.superviseVisible = false;
    this.workflowid = '';
    this.operationType = 0;
    this.requestid = '';
  }

  @action.bound
  saveSupervise(getTableDatas, getTabData) {
    const formParams = this.superviseForm.getFormParams();
    const remindSetingFormParams = this.remindSetingForm.getFormParams();
    const params = {
      ...formParams,
      ...remindSetingFormParams,
      requestid: this.requestid,
      operationType: this.operationType,
    };
    Api.saveSuperviseOperation(params).then((data) => {
      if (data.api_status) {
        this.superviseHide();
        getTableDatas({}, false, false);
        getTabData();
        message.success(getLabel('387108', '督办成功'));
      }
    });
  }

  @action.bound
  saveUrge() {
    const params = {
      requestid: this.requestid,
      operationType: this.operationType,
      superviseid: this.superviseid,
      ...this.urgeForm.getFormParams(),
    };
    Api.saveSuperviseOperation(params).then((data) => {
      if (data.api_status) {
        this.urgeHide();
        message.success(getLabel('517321', '催办成功'));
      }
    });
  }

  @action.bound
  setMobxState(params = {}) {
    Object.keys(params).map((key) => {
      this[key] = params[key];
    });
  }

  @action.bound
  urgeHide() {
    this.urgeVisible = false;
  }

  @action.bound
  validateForm(form, callBack, getTableDatas = null, getTabData = null) {
    form.validateForm().then((f) => {
      if (f.isValid) {
        callBack(getTableDatas, getTabData);
      } else {
        f.showErrors();
      }
    });
  }

  @action.bound
  validateRemindSetingForm() {
    const superviseParams = this.superviseForm.getFormParams();
    const remindSetingParams = this.remindSetingForm.getFormParams();
    if (superviseParams.superviseRemindType.includes('0')) {
      if (remindSetingParams.beforeRemindType === '' || remindSetingParams.beforeRemindNum === '' || remindSetingParams.beforeRemindTime === '') {
        message.warn(getLabel('508377', '有必填项没有填写'));
        return;
      }
    }
    if (superviseParams.superviseRemindType.includes('1')) {
      if (remindSetingParams.afterRemindType === '' || remindSetingParams.afterRemindNum === '' || remindSetingParams.afterRemindTime === '') {
        message.warn(getLabel('508377', '有必填项没有填写'));
        return;
      }
    }
    this.remindSetingHide();
  }

  @action.bound
  getDelayDateCondtion() {
    const params = {
      operationType: this.operationType,
      requestid: this.requestid,
    };
    Api.getSuperviseOperation(params).then((data) => {
      if (data.conditions) {
        this.delayRequestForm = new WeaForm();
        this.delayRequestForm.setCondition(data.conditions);
        this.delayRequestVisible = true;
      }
    });
  }

  @action.bound
  checkChange(type) {
    const superviseRemindType = this.superviseForm.getFormParams().superviseRemindType;
    let values = '';
    if (type == 'pre') {
      values = superviseRemindType.includes('0') && superviseRemindType.includes('1') ? '1' :
        !superviseRemindType.includes('0') && superviseRemindType.includes('1') ? '0,1' :
          superviseRemindType.includes('0') && !superviseRemindType.includes('1') ? '' : '0';
    } else {
      values = superviseRemindType.includes('0') && superviseRemindType.includes('1') ? '0' :
        !superviseRemindType.includes('0') && superviseRemindType.includes('1') ? '' :
          superviseRemindType.includes('0') && !superviseRemindType.includes('1') ? '0,1' : '1';
    }
    this.superviseForm.updateFields({ superviseRemindType: { value: values } });
  }

  @action.bound
  remindSetingHide() {
    this.remindSetingVisible = false;
  }

  @action.bound
  processOpinion(requestid, workflowid) {
    const params = {
      requestid,
      workflowid,
    };
    Api.getCirculationOpinions(params).then((data) => {
      if (data.loglist) {
        this.opinionList = data.loglist;
        this.processOpinionVisible = true;
      }
    });
  }

  @action.bound
  processOpinionHide() {
    this.processOpinionVisible = false;
  }

  @action.bound
  getCustomPageListStatus(getTableDatas, getSearchFormItem, searchParams = {}, customTabId = 0) {
    const params = {
      ...searchParams,
      customListId: this.customListId,
    };
    Api.getCustomPageListStatus(params).then((data) => {
      if (data.api_status) {
        this.isOpen = data.isOpen;
        this.pageTitle = data.pageTitle;
        if (data.isOpen) {
          this.getTopButton(customTabId, searchParams, getTableDatas, getSearchFormItem);
        }
      }
    });
  }

  @action.bound
  async getTopButton(customTabId = 0, searchParams = {}, getTableDatas, getSearchFormItem) {
    this.customTabId = customTabId;
    const params = {
      customListId: this.customListId,
      customTabId,
    };
    const resData = await Api.getCustomBatchBtn(params);
    if (resData.api_status) {
      this.getButtonType = resData.btnType || '';
      const btnType = resData.btnType && resData.btnType.split(',')[0];
      this.buttonType = btnType;
      const formParams = await getSearchFormItem(searchParams);
      getTableDatas && getTableDatas({
        btnType,
        ...formParams,
        ...searchParams,
        customListId: this.customListId,
        customTabId,
      },true);
    }
  }

  @action.bound
  getBatchLimit() {
    const params = {
      method: 'all',
      officalType: '-1',
      viewScope: 'doing',
      complete: '0',
      menuIds: '1,13',
      viewcondition: '0',
      actiontype: 'splitpage',
      offical: '',
    };
    Api.getBatchLimit(params).then((data) => {
      this.sharearg = data.sharearg;
    });
  }

  @action.bound
  secondAuth(remark, getDatas, getTabData) {
    // 二次验证 流程主干代码和分支代码差距有点大  先不做二次验证  --kl2018

    // let requestIds = selectedKeys.join(",");
    // Api.getSecondAuthConfig4List({requestIds}).then(checkResult => {
    //   const {isEnableAuth = 0, secondAuthType = 0, authKey = ''} = checkResult;
    //   if (isEnableAuth == 1) {      //需要二次验证
    //       wfListDoingStore.setAuthCallback(this.batchSubmitEvent0.bind(this));
    //       wfListDoingStore.setAuthCallbackParam(remark);
    //       wfListDoingStore.setSecondAuthType(secondAuthType);
    //       wfListDoingStore.setSecondAuthKey(authKey);
    //       wfListDoingStore.setShowSecondAuth(true);
    //   } else {     //不需要，提交签字意见
    //       this.batchSubmitEvent0(remark);
    //   }
    // });


    this.batchSubmitEvent(remark, getDatas, getTabData);
  }

  @action.bound
  batchSubmitEvent(remark, getDatas, getTabData) {
    const belongtoUserArr = this.selectedDatas.map(item => item.createrid);
    const params = {
      multiSubIds: this.selectedKeys.join(','),
      remark,
      pagefromtype: 1,
      belongtoUserids: belongtoUserArr.join(','),
      needBatchSubmit: 1,
    };

    Api.requestBatchSubmit(params).then((data) => {
      if (data.successRequestIds) {
        this.selectedKeys = [];
        message.success(getLabel('16248', '提交成功'));
        getDatas && getDatas();
        getTabData && getTabData();
        this.showBatchSubmit = false;
      }
    });
  }

  @action.bound
  initState() {
    this.isOpen = true; // 是否启用地址
    this.customListId = '';// 自定义页面id
    this.customTabId = '';
    this.pageTitle = '';// 页面标题
    this.getButtonType = '';
    this.buttonType = '';
    this.sharearg = {};// 批量提交相关权限
    this.showBatchSubmit = false;
    this.selectedKeys = [];
    this.selectedDatas = [];
  }
}
